package com.dianping.cat.consumer.dependency.model.transform;

import com.dianping.cat.consumer.dependency.model.IEntity;
import com.dianping.cat.consumer.dependency.model.IVisitor;
import com.dianping.cat.consumer.dependency.model.entity.Dependency;
import com.dianping.cat.consumer.dependency.model.entity.DependencyReport;
import com.dianping.cat.consumer.dependency.model.entity.Index;
import com.dianping.cat.consumer.dependency.model.entity.Segment;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;




















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  43 */   public DefaultXmlBuilder() { this(false); }



/*  47 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  51 */     this.m_compact = compact;
/*  52 */     this.m_sb = sb;
/*  53 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  57 */     entity.accept(this.m_visitor);
/*  58 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  62 */     this.m_level--;

/*  64 */     indent();
/*  65 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  69 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  73 */     if (value == null) {
/*  74 */       return null;
    }

/*  77 */     String str = toString(value);
    int len = str.length();
/*  79 */     StringBuilder sb = new StringBuilder(len + 16);

/*  81 */     for (int i = 0; i < len; i++) {
/*  82 */       char ch = str.charAt(i);

/*  84 */       switch (ch) {
        case '<':
/*  86 */           sb.append("&lt;");
          break;
        case '>':
/*  89 */           sb.append("&gt;");
          break;
        case '&':
/*  92 */           sb.append("&amp;");
          break;
        case '"':
/*  95 */           if (!text) {
/*  96 */             sb.append("&quot;");
            break;
          }
        default:
/* 100 */           sb.append(ch);
          break;
      }

    }
/* 105 */     return sb.toString();
  }

  protected void indent() {
/* 109 */     if (!this.m_compact) {
/* 110 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 111 */         this.m_sb.append("   ");
      }
    }
  }


/* 117 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 121 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 125 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 129 */     indent();

/* 131 */     this.m_sb.append('<').append(name);

/* 133 */     int len = nameValues.length;

/* 135 */     for (int i = 0; i + 1 < len; i += 2) {
/* 136 */       Object attrName = nameValues[i];
/* 137 */       Object attrValue = nameValues[i + 1];

/* 139 */       if (attrValue != null) {
/* 140 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 144 */     if (dynamicAttributes != null) {
/* 145 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 146 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 150 */     if (text != null && closed) {
/* 151 */       this.m_sb.append('>');
/* 152 */       this.m_sb.append(escape(text, true));
/* 153 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 155 */       if (closed) {
/* 156 */         this.m_sb.append('/');
      } else {
/* 158 */         this.m_level++;
      }

/* 161 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 167 */     if (value instanceof String)
/* 168 */       return (String)value; 
/* 169 */     if (value instanceof Collection) {
/* 170 */       Collection<Object> list = (Collection)value;
/* 171 */       StringBuilder sb = new StringBuilder(32);
/* 172 */       boolean first = true;

/* 174 */       for (Object item : list) {
/* 175 */         if (first) {
/* 176 */           first = false;
        } else {
/* 178 */           sb.append(',');
        }

/* 181 */         if (item != null) {
/* 182 */           sb.append(item);
        }
      }

/* 186 */       return sb.toString();
/* 187 */     }  if (value.getClass().isArray()) {
/* 188 */       int len = Array.getLength(value);
/* 189 */       StringBuilder sb = new StringBuilder(32);
/* 190 */       boolean first = true;

/* 192 */       for (int i = 0; i < len; i++) {
/* 193 */         Object item = Array.get(value, i);

/* 195 */         if (first) {
/* 196 */           first = false;
        } else {
/* 198 */           sb.append(',');
        }

/* 201 */         if (item != null) {
/* 202 */           sb.append(item);
        }
      }

/* 206 */       return sb.toString();
    }

/* 209 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 213 */     if (text == null) {
      return;
    }

/* 217 */     indent();

/* 219 */     this.m_sb.append('<').append(name);

/* 221 */     int len = nameValues.length;

/* 223 */     for (int i = 0; i + 1 < len; i += 2) {
/* 224 */       Object attrName = nameValues[i];
/* 225 */       Object attrValue = nameValues[i + 1];

/* 227 */       if (attrValue != null) {
/* 228 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 232 */     this.m_sb.append(">");
/* 233 */     this.m_sb.append(escape(text, true));
/* 234 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 238 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 242 */     indent();

/* 244 */     this.m_sb.append('<').append(name).append(">");

/* 246 */     if (escape) {
/* 247 */       this.m_sb.append(escape(text, true));
    } else {
/* 249 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 252 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 256 */     if (date != null) {
/* 257 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 259 */     return null;
  }


  protected String toString(Number number, String format) {
/* 264 */     if (number != null) {
/* 265 */       return (new DecimalFormat(format)).format(number);
    }
/* 267 */     return null;
  }




/* 273 */   public void visitDependency(Dependency dependency) { startTag("dependency", true, null, new Object[] { "type", dependency.getType(), "target", dependency.getTarget(), "total-count", Long.valueOf(dependency.getTotalCount()), "avg", toString(Double.valueOf(dependency.getAvg()), "0.00"), "error-count", Long.valueOf(dependency.getErrorCount()), "key", dependency.getKey(), "sum", toString(Double.valueOf(dependency.getSum()), "0.00") }); }



  public void visitDependencyReport(DependencyReport dependencyReport) {
/* 278 */     startTag("dependency-report", null, new Object[] { "domain", dependencyReport.getDomain(), "startTime", toString(dependencyReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(dependencyReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 280 */     if (!dependencyReport.getDomainNames().isEmpty()) {
/* 281 */       for (String domainName : dependencyReport.getDomainNames()) {
/* 282 */         tagWithText("domainName", domainName, new Object[0]);
      }
    }

/* 286 */     if (!dependencyReport.getSegments().isEmpty()) {
/* 287 */       for (Segment segment : dependencyReport.getSegments().values()) {
/* 288 */         segment.accept(this.m_visitor);
      }
    }

/* 292 */     endTag("dependency-report");
  }



/* 297 */   public void visitIndex(Index index) { startTag("index", true, null, new Object[] { "name", index.getName(), "total-count", Long.valueOf(index.getTotalCount()), "error-count", Long.valueOf(index.getErrorCount()), "sum", toString(Double.valueOf(index.getSum()), "0.00"), "avg", toString(Double.valueOf(index.getAvg()), "0.00") }); }



  public void visitSegment(Segment segment) {
/* 302 */     startTag("segment", null, new Object[] { "id", segment.getId(), "exception-count", segment.getExceptionCount() });

/* 304 */     if (!segment.getIndexs().isEmpty()) {
/* 305 */       for (Index index : segment.getIndexs().values()) {
/* 306 */         index.accept(this.m_visitor);
      }
    }

/* 310 */     if (!segment.getDependencies().isEmpty()) {
/* 311 */       for (Dependency dependency : segment.getDependencies().values()) {
/* 312 */         dependency.accept(this.m_visitor);
      }
    }

/* 316 */     endTag("segment");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/dependency/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */